package br.com.projeto.DAO;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import br.com.projeto.DB;
import br.com.projeto.VO.ClienteVO;

public class ClienteDAO {

	private String TABLE_NAME = "cliente";
	private String[] COLUMNS = {"id","nome","email","endereco","telefone"};
	private Context ctx;

	public ClienteDAO(Context ctx) {
		// TODO Auto-generated constructor stub
		this.ctx = ctx;
	}

	public boolean insert(ClienteVO cliente) {
		SQLiteDatabase db = new DB(ctx).getWritableDatabase();
		ContentValues ctv = new ContentValues();
		ctv.put("nome", cliente.getNome());
		ctv.put("email", cliente.getEmail());
		ctv.put("endereco", cliente.getEndereco());
		ctv.put("telefone", cliente.getTelefone());

		boolean retorno = (db.insert(TABLE_NAME, null, ctv) > 0);

		db.close();
		return retorno;
	}

	public boolean delete(ClienteVO cliente) {
		SQLiteDatabase db = new DB(ctx).getWritableDatabase();
		boolean retorno = (db.delete(TABLE_NAME, "id=?", new String[]{cliente.getId().toString()})>0);
		db.close();
		return retorno;
	}

	public boolean update(ClienteVO cliente) {
		SQLiteDatabase db = new DB(ctx).getWritableDatabase();
		ContentValues ctv = new ContentValues();
		ctv.put("nome", cliente.getNome());
		ctv.put("email", cliente.getEmail());
		ctv.put("endereco", cliente.getEndereco());
		ctv.put("telefone", cliente.getTelefone());
		
		boolean retorno = (db.update(TABLE_NAME, ctv, "id=?", new String[]{cliente.getId().toString()})>0);
		db.close();
		return retorno;
	}

	public ClienteVO getById(Integer ID) {
		SQLiteDatabase db = new DB(ctx).getReadableDatabase();
		Cursor rs = db.query(TABLE_NAME, COLUMNS, "id=?",new String[]{ID.toString()},null,null,null);
		ClienteVO cliente = null;
		if(rs.moveToFirst()){
			cliente = new ClienteVO();
			cliente.setId(rs.getInt(rs.getColumnIndex("id")));
			cliente.setNome(rs.getString(rs.getColumnIndex("nome")));
			cliente.setEmail(rs.getString(rs.getColumnIndex("email")));
			cliente.setEndereco(rs.getString(rs.getColumnIndex("endereco")));
			cliente.setTelefone(rs.getString(rs.getColumnIndex("telefone")));
		}
		db.close();
		return cliente;

	}

	public List<ClienteVO> getAll() {
		SQLiteDatabase db = new DB(ctx).getReadableDatabase();
		Cursor rs = db.rawQuery("select * from cliente", null);
		List<ClienteVO> list = new ArrayList<ClienteVO>();
		ClienteVO cliente = null;
		while (rs.moveToNext()) {
			cliente = new ClienteVO();
			cliente.setId(rs.getInt(rs.getColumnIndex("id")));
			cliente.setNome(rs.getString(rs.getColumnIndex("nome")));
			cliente.setEmail(rs.getString(rs.getColumnIndex("email")));
			cliente.setEndereco(rs.getString(rs.getColumnIndex("endereco")));
			cliente.setTelefone(rs.getString(rs.getColumnIndex("telefone")));
			list.add(cliente);
		}
		db.close();
		return list;
	}
}
